// File Name: flash_task.c
// Author: kukukukuku~
// Date: 2024-07-12

#include "./User/Flash/inc/flash_task.h"
#include "./User/Key/inc/key_ctrl.h"
#include "./User/LED/inc/led_ctrl.h"
#include "./User/OLED/inc/oled_ctrl.h"
#include "./User/RS485/inc/rs485_ctrl.h"
#include "./User/Step_Motor/inc/step_motor_ctrl.h"
#include "./User/Sys/inc/init.h"
#include "./User/WS2812B/inc/ws2812b_ctrl.h"

TaskHandle_t taskHandleFlash;   //Flash任务句柄

SemaphoreHandle_t semaphBinFlash;   //Flash二值信号量

/**
 * @brief   Flash任务
 * @param   arg:    任务参数
 * @retval  None
 * @note    None
*/
void task_flash(void *arg)
{
    //上电读取失败则自动保存
    if(flashReadFlag)
    {
        xSemaphoreGive(semaphBinFlash);
    }
    
    while(1)
    {
        if(xSemaphoreTake(semaphBinFlash, portMAX_DELAY) == pdTRUE)
        {
            flashData[FLASH_DATA_KEY_CLICK_BEEP_INDEX]        = keyClickBeep;
            flashData[FLASH_DATA_KEY_LONG_BEEP_INDEX]         = keyLongBeep;
            flashData[FLASH_DATA_LED1_MODE_INDEX]             = led1.mode;
            flashData[FLASH_DATA_LED1_BLINK_FREQ_INDEX]       = led1.blinkNum;
            flashData[FLASH_DATA_LED1_BREATH_STEP_INDEX]      = led1.breathStep;
            flashData[FLASH_DATA_LED2_MODE_INDEX]             = led2.mode;
            flashData[FLASH_DATA_LED2_BLINK_FREQ_INDEX]       = led2.blinkNum;
            flashData[FLASH_DATA_LED2_BREATH_STEP_INDEX]      = led2.breathStep;
            flashData[FLASH_DATA_LED1_SWITCH]                 = led1Switch;
            flashData[FLASH_DATA_LED2_SWITCH]                 = led2Switch;
            flashData[FLASH_DATA_OLED_DIR_INDEX]              = oledDir;
            flashData[FLASH_DATA_OLED_LIGHT_INDEX]            = oledLightLevel;
            flashData[FLASH_DATA_OLED_CURSOR_INDEX]           = oledCursorStyle;
            flashData[FLASH_DATA_OLED_REFRESH_MS_INDEX]       = oledMainPageRefreshInterval;
            flashData[FLASH_DATA_OLED_MENU_NUMBER_INDEX]      = oledMenuNumberFlag;
            flashData[FLASH_DATA_STEP_MOTOR_MODE_INDEX]       = stepMotorMode;
            flashData[FLASH_DATA_STEP_MOTOR_SPEED_INDEX]      = stepMotorSpd;
            flashData[FLASH_DATA_WS2812B_MODE_INDEX]          = ws2812bMode;
            flashData[FLASH_DATA_WS2812B_STOP_R_INDEX]        = ws2812bStopRed;
            flashData[FLASH_DATA_WS2812B_STOP_G_INDEX]        = ws2812bStopGreen;
            flashData[FLASH_DATA_WS2812B_STOP_B_INDEX]        = ws2812bStopBlue;
            flashData[FLASH_DATA_WS2812B_CW_MODE_INDEX]       = ws2812bCwMode;
            flashData[FLASH_DATA_WS2812B_CW_R_INDEX]          = ws2812bCwRed;
            flashData[FLASH_DATA_WS2812B_CW_G_INDEX]          = ws2812bCwGreen;
            flashData[FLASH_DATA_WS2812B_CW_B_INDEX]          = ws2812bCwBlue;
            flashData[FLASH_DATA_WS2812B_CCW_MODE_INDEX]      = ws2812bCcwMode;
            flashData[FLASH_DATA_WS2812B_CCW_R_INDEX]         = ws2812bCcwRed;
            flashData[FLASH_DATA_WS2812B_CCW_G_INDEX]         = ws2812bCcwGreen;
            flashData[FLASH_DATA_WS2812B_CCW_B_INDEX]         = ws2812bCcwBlue;
            flashData[FLASH_DATA_RS485_MODE]                  = rs485Mode;
            flashData[FLASH_DATA_RS485_ID]                    = rs485Id;
            flashData[FLASH_DATA_RS485_BAUDRATE_H]            = (uint16_t)(rs485UsartBaudRateNew >> 8 & 0xFFFF);
            flashData[FLASH_DATA_RS485_BAUDRATE_L]            = (uint16_t)(rs485UsartBaudRateNew & 0xFFFF);
            flashData[FLASH_DATA_RS485_ACTIVE_INTERVAL_INDEX] = rs485ActiveInterval;
            flashData[FLASH_DATA_VERSION_H]                   = VERSION_MAIN;
            flashData[FLASH_DATA_VERSION_L]                   = VERSION_SECOND << 8 | VERSION_PHASE;
            
            flash_data_save(FLASH_ADDR_DATA_START, flashData, FLASH_DATA_NUM) == 0 ?\
            (flashSaveFlag = 1) : (flashSaveFlag = 2);
            
            if(rs485SaveFlag == 1)
            {
                rs485SaveFlag += flashSaveFlag;
            }
        }
    }
}
